package com.ruoyi.develop.lvDaohuoDetail.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.export.service.impl.ExportServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.lvDaohuoDetail.domain.LvDaohuoDetail;
import com.ruoyi.develop.lvDaohuoDetail.service.ILvDaohuoDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 到货明细Controller
 * @author lmm
 * @date 2024-10-07
 */
@RestController
@RequestMapping("/lvDaohuoDetail/lvDaohuoDetail")
public class LvDaohuoDetailController extends BaseController {

    @Autowired
    private ILvDaohuoDetailService lvDaohuoDetailService;
    @Autowired
    private ExportServiceImpl exportService;

    /**
     * 查询到货明细列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuoDetail:query')")
    @GetMapping("/list")
    public TableDataInfo list(LvDaohuoDetail entity) {
        startPage();
        List<LvDaohuoDetail> list = lvDaohuoDetailService.selectList(entity);
        return getDataTable(list);
    }


    /**
     * 查询到货明细合并列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuoDetail:query')")
    @GetMapping("/listMerge")
    public TableDataInfo listMerge(LvDaohuoDetail entity) {
        startPage();
        List<LvDaohuoDetail> list = lvDaohuoDetailService.selectListMerge(entity);
        return getDataTable(list);
    }



    @PreAuthorize("@ss.hasPermi('develop:LvDaohuoDetail:query')")
    @GetMapping("/listTuihuo")
    public TableDataInfo listTuihuo(LvDaohuoDetail entity) {
        startPage();
        List<LvDaohuoDetail> list = lvDaohuoDetailService.listTuihuo(entity);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('develop:LvDaohuoDetail:query')")
    @GetMapping("/listWeiruku")
    public TableDataInfo listWeiruku(LvDaohuoDetail entity) {
        startPage();
        List<LvDaohuoDetail> list = lvDaohuoDetailService.listWeiruku(entity);
        return getDataTable(list);
    }
    /**
     * 导出到货明细列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuoDetail:export')")
    @Log(title = "到货明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LvDaohuoDetail entity) {
        List<LvDaohuoDetail> list = lvDaohuoDetailService.selectList(entity);
        ExcelUtil<LvDaohuoDetail> util = new ExcelUtil<LvDaohuoDetail>(LvDaohuoDetail. class);
        util.exportExcel(response, list, "到货明细数据");
    }

    /**
     * 获取到货明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuoDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lvDaohuoDetailService.selectById(id));
    }

    /**
     * 新增到货明细
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuoDetail:add')")
    @Log(title = "到货明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LvDaohuoDetail entity) {
        return toAjax(lvDaohuoDetailService.insert(entity));
    }

    /**
     * 修改到货明细
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuoDetail:edit')")
    @Log(title = "到货明细", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LvDaohuoDetail entity) {
        return toAjax(lvDaohuoDetailService.update(entity));
    }

    @PreAuthorize("@ss.hasPermi('develop:LvDaohuoDetail:edit')")
    @Log(title = "批量质检", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/zhijianDaohuoDetail")
    public AjaxResult zhijianDaohuoDetail(HttpServletRequest request) {
        String ids  = request.getParameter("ids");
        String state  = request.getParameter("state");
        for(String id :ids.split(",")){
            LvDaohuoDetail lvDaohuoDetail=lvDaohuoDetailService.selectById(id);
            lvDaohuoDetail.setInspectDate(new Date());
            lvDaohuoDetail.setInspectName(getLoginUser().getUsername());
            lvDaohuoDetail.setInspectState(state);
            lvDaohuoDetailService.update(lvDaohuoDetail);
        }
        return AjaxResult.success("质检完成！");
    }

    /**
     * 删除到货明细
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuoDetail:remove')")
    @Log(title = "到货明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lvDaohuoDetailService.deleteByIds(ids));
    }


    /**
     * 到货明细
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuoDetail:remove')")
    @Log(title = "到货明细", businessType = BusinessType.DELETE)
    @GetMapping("/ruku/{ids}")
    public AjaxResult ruku(@PathVariable String[] ids) {
        int i = lvDaohuoDetailService.rukuByDaohuoDetail(ids);
        return AjaxResult.success(i);
    }


    @Log(title = "全部销售信息导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LvDaohuoDetail entity, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<>();
        String date = DateUtils.getTime();//默认当天
        startPage();
        List<LvDaohuoDetail> list = lvDaohuoDetailService.selectList(entity);
        list.stream().forEach(e->{
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("createTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, e.getCreateTime()));
            e.setParams(map);
        });
        params.put("list", list);
        params.put("date", date);
        String fileName = request.getParameter("fileName");
        if(StringUtils.isBlank(fileName)){
            fileName = "产品到货明细.xlsx";
        }
        String templateName = "产品到货明细.xlsx";
        exportService.baseXlsxExport(fileName, params, templateName, request, response);
        return null;
    }






}
